<script lang="ts" setup>
import type { AiTrainingDatasetMetaApi } from '#/api/ai/dataset/trainingDatasetMeta';

import { ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';

import { getTrainingDatasetMeta } from '#/api/ai/dataset/trainingDatasetMeta';

import Samples from '../../trainingDatasetSamples/index.vue';

const formData = ref<AiTrainingDatasetMetaApi.TrainingDatasetMeta>({});
const [Drawer, drawerApi] = useVbenDrawer({
  footer: false,
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = {};
      return;
    }
    // 加载数据
    let data =
      drawerApi.getData<AiTrainingDatasetMetaApi.TrainingDatasetMeta>();
    if (!data || !data.id) {
      formData.value = data;
      return;
    }
    if (data.id) {
      drawerApi.lock();
      try {
        data = await getTrainingDatasetMeta(data.id);
      } finally {
        drawerApi.unlock();
      }
    }
    // 设置到 values
    formData.value = data;
  },
});
</script>

<template>
  <Drawer title="样本集管理" class="w-[80%]">
    <Samples
      v-if="formData.id"
      :dataset-id="formData.id"
      :version-id="formData.versionId"
    />
  </Drawer>
</template>
